'use strict'

import Store from '../../base/Store'
import constants from './constants'
import dispatcher from './dispatcher'
import filter from 'lodash/filter'
import noop from 'lodash/noop'

const state = {
    tasks: {},
    counts: {
        new: 0,
        todo: 0,
        done: 0
    }
}

class TaskStore extends Store {
    constructor() {
        super()
        dispatcher.register(action => {
            switch (action.actionType) {
                case constants.ACCEPT:
                    this.commitAccept(action.id, action.success)
                    break;
                case constants.REJECT:
                    this.commitReject(action.id, action.reason, action.success)
                    break;
                case constants.CANCEL:
                    this.commitCancel(action.id, action.reason, action.success)
                    break;
                case constants.COMPLETE:
                    this.commitComplete(action.id, action.success)
                    break;
                case constants.RESCHEDULE:
                    this.commitReschedule(action.id, action.time, action.success)
                    break;
            }
        })
    }

    commitAccept(id, success = noop) {
        Store.apiGetJSON('ACCEPT_TASK', {
            task_id: id
        }).then((res) => {
                if (res.success) {
                    this.fetchList()
                    this.fetchCount()
                    success()
                }
            })

    }

    commitReject(id, reason, success = noop) {
        Store.apiPost('CANCEL_TASK', {
            task_id: id,
            type: 1,
            cancel_why: reason
        }).then((res) => {
                if (res.success) {
                    this.fetchList()
                    this.fetchCount()
                    success()
                }
            })
    }

    commitCancel(id, reason, success = noop) {
        Store.apiPost('CANCEL_TASK', {
            task_id: id,
            type: 2,
            cancel_why: reason
        }).then((res) => {
            if (res.success) {
                this.fetchList()
                this.fetchCount()
                success()
            }
        })
    }

    commitComplete(id, success = noop) {
        Store.apiPost('COMPLETE_TASK', {
            task_id: id
        }).then((res) => {
                if (res.success) {
                    this.fetchList()
                    this.fetchCount()
                    success()
                }
            })
    }

    commitReschedule(id, time, success = noop) {
        Store.apiPost('ADJUST_TIME', Object.assign({
            task_id: id
        }, time)).then(res => {
            if (res.success) {
                this.fetchList()
                this.fetchCount()
                success()
            }
        })
    }

    fetchList(status = '', success = noop) {
        Store.apiGetJSON('GET_TASK_LIST', {
            arrange_status: status
        }).then(res => {
            if (res.success) {
                state.tasks = {}
                res.data.rows.map((row) => {
                    state.tasks[row._id] = row
                })
                this.emitChange()
                success()
            }
        })
    }

    fetchCount() {
        Store.apiGetJSON('GET_TASK_COUNT')
        .then(res => {
                if (res.success) {
                    state.counts = res.data.counts
                    this.emitCountChange()
                }
            })
    }

    fetchItem(id) {
        return Store.apiGetJSON('GET_TASK_DETAIL', {
            task_id: id
        }).then(res => {
            if (res.success) {
                state.tasks[id] = res.data
                this.emitChange()
            }
        })
    }

    calcBalance(id) {
        const task = this.getItem(id)
        const total = task.door_pay_money + task.balance_pay_money + task.ticket_amount_total
        const income = total - task.rebate_money
        return {
            total: total,
            rebate: task.rebate_money,
            income: income
        }
    }

    getList(status) {
        return filter(state.tasks, {
            arrange_status: status
        })
    }

    getItem(id) {
        return state.tasks[id]
    }

    getCounts() {
        return state.counts
    }

    emitInit() {
        this.inited = true;
        this.emit(constants.INIT)
    }

    isInited() {
        return this.inited;
    }

    emitChange() {
        this.emit(constants.CHANGE)
    }

    emitCountChange() {
        this.emit(constants.COUNT_CHANGE)
    }

    addCountChangeListener(callback) {
        this.on(constants.COUNT_CHANGE, callback)
    }

    removeCountChangeListener(callback) {
        this.removeListener(constants.COUNT_CHANGE, callback)
    }

    addInitListener(callback) {
        this.on(constants.INIT, callback)
    }

    removeInitListener(callback) {
        this.removeListener(constants.INIT, callback)
    }

    addChangeListener(callback) {
        this.on(constants.CHANGE, callback)
    }

    removeChangeListener(callback) {
        this.removeListener(constants.CHANGE, callback)
    }

    getInitialState() {
        this.fetchList('', () => {
            this.emitInit()
        })
        this.fetchCount()
    }
}

export default new TaskStore()
